//
//  ShowDaySignInViewController.m
//  1gene
//
//  Created by 1jiyin on 16/5/25.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "ShowDaySignInViewController.h"
#define SignInViewTag 11122
#import "AFNetWorking.h"



@interface ShowDaySignInViewController ()

@end

@implementation ShowDaySignInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:[UIView new]];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createUI];
    // Do any additional setup after loading the view.
}

-(void)createUI
{
    [self createSignView:[NSString stringWithFormat:@"%@",_days]];
    
    // 取消返回时的阴影
    UIImageView *llll =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    llll.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:llll];
}


-(void)createSignView:(NSString *)days
{
    NSInteger day=[days integerValue];
        UIView *view=[[UIView alloc]initWithFrame:self.view.bounds];
        view.tag=SignInViewTag;
        view.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:view];
        
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*(250/375.0))];
        imgView.image=[UIImage imageNamed:@"signin1"];
        [view addSubview:imgView];
        
        UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imgView.frame)+25, self.view.frame.size.width, 25)];
        lab1.text=[NSString stringWithFormat:@"坚持关爱健康%ld天!",day+1];
        lab1.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
        lab1.textAlignment=NSTextAlignmentCenter;
        lab1.font=[UIFont systemFontOfSize:22];
        [view addSubview:lab1];
        
        NSArray *arr=@[@{@"content1":@"健康币+1",@"content2":@"今天"},@{@"content1":@"健康币+3",@"content2":@"7天"},@{@"content1":@"健康币+10",@"content2":@"1个月"},@{@"content1":@"健康币+50",@"content2":@"3个月"},@{@"content1":@"健康币+80",@"content2":@"6个月"},@{@"content1":@"健康币+120",@"content2":@"9个月"}];
        
        CGFloat width=(self.view.frame.size.width-30*4)/3.0;
        CGFloat height=width*(110/144.0);
        CGFloat img_w=width*(50/144.0);
        CGFloat img_h=height*(53/110.0);
        
        UIButton *sureBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame=CGRectMake((self.view.frame.size.width*(1-336/750.0))/2.0, CGRectGetMaxY(lab1.frame)+20+(height+50)*2, self.view.frame.size.width*(336/750.0), self.view.frame.size.width*(67/750.0));
        [sureBtn setBackgroundImage:[UIImage imageNamed:@"signin2"] forState:UIControlStateNormal];
        [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureBtn.titleLabel.font=[UIFont systemFontOfSize:22];
        [sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:sureBtn];
        
        UILabel *attentionLab=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(sureBtn.frame)+10, self.view.frame.size.width, 20)];
        attentionLab.text=@"*  健康币可以用于抽奖";
        attentionLab.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
        attentionLab.textAlignment=NSTextAlignmentCenter;
        attentionLab.font=[UIFont systemFontOfSize:16];
        [view addSubview:attentionLab];
        
        //提示
    UILabel *attentionLab2=[[UILabel alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-30, self.view.frame.size.width, 30)];
    attentionLab2.text=@"   提示:坚持签到会有不同的惊喜哟";
    attentionLab2.textColor=[UIColor whiteColor];
    attentionLab2.font=[UIFont boldSystemFontOfSize:14];
    attentionLab2.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
    [view addSubview:attentionLab2];
    
        
        
        for (int i=0; i<2; i++) {
            
            for (int j=0; j<3; j++) {
                UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(30+(width+30)*j, CGRectGetMaxY(lab1.frame)+20+(height+50)*i, width, height+30)];
                [view addSubview:view1];
                
                UIView *subView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
                [view1 addSubview:subView];
                
                UIImageView *imgView1=[[UIImageView alloc]initWithFrame:CGRectMake((width-img_w)/2.0, 5, img_w, img_h)];
                [subView addSubview:imgView1];
                
                UILabel *content1=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imgView1.frame), width, height-img_h-5)];
                content1.text=arr[i*3+j][@"content1"];
                content1.font=[UIFont systemFontOfSize:14];
                content1.textAlignment=NSTextAlignmentCenter;
                [subView addSubview:content1];
                
                UILabel *content2=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(subView.frame), width,28)];
                content2.text=arr[i*3+j][@"content2"];
                content2.font=[UIFont systemFontOfSize:16];
                content2.textAlignment=NSTextAlignmentCenter;
                [view1 addSubview:content2];
                
                
                
                subView.layer.borderColor=[UIColor colorWithRed:189/255.0 green:192/255.0 blue:191/255.0 alpha:1].CGColor;
                subView.layer.borderWidth=2.0;
                subView.layer.cornerRadius=5.0;
                subView.backgroundColor=[UIColor colorWithRed:189/255.0 green:192/255.0 blue:191/255.0 alpha:1];
                
                
                imgView1.image=[UIImage imageNamed:@"signin4"];
                content1.textColor=[UIColor whiteColor];
                content2.textColor=[UIColor colorWithRed:189/255.0 green:192/255.0 blue:191/255.0 alpha:1];
                
                
                if (i*3+j==0) {
                    subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                    subView.layer.borderWidth=2.0;
                    subView.layer.cornerRadius=5.0;
                    subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    
                    
                    imgView1.image=[UIImage imageNamed:@"signin3"];
                    content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                    content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    
                }
                if (day>=6&&day<29) {
                    if (i*3+j==1) {
                        subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                        subView.layer.borderWidth=2.0;
                        subView.layer.cornerRadius=5.0;
                        subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        
                        
                        imgView1.image=[UIImage imageNamed:@"signin3"];
                        content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                        content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    }
                    
                }
                if (day>=29&&day<89) {
                    if (i*3+j==1||i*3+j==2) {
                        subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                        subView.layer.borderWidth=2.0;
                        subView.layer.cornerRadius=5.0;
                        subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        
                        
                        imgView1.image=[UIImage imageNamed:@"signin3"];
                        content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                        content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    }
                }
                
                if (day>=89&&day<179) {
                    if (i*3+j==1||i*3+j==2||i*3+j==3) {
                        subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                        subView.layer.borderWidth=2.0;
                        subView.layer.cornerRadius=5.0;
                        subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        
                        
                        imgView1.image=[UIImage imageNamed:@"signin3"];
                        content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                        content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    }
                }
                
                if (day>=179&&day<269) {
                    if (i*3+j==1||i*3+j==2||i*3+j==3||i*3+j==4) {
                        subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                        subView.layer.borderWidth=2.0;
                        subView.layer.cornerRadius=5.0;
                        subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        
                        
                        imgView1.image=[UIImage imageNamed:@"signin3"];
                        content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                        content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    }
                }
                
                
                if (day==269) {
                    if (i*3+j==1||i*3+j==2||i*3+j==3||i*3+j==4||i*3+j==5) {
                        subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                        subView.layer.borderWidth=2.0;
                        subView.layer.cornerRadius=5.0;
                        subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        
                        
                        imgView1.image=[UIImage imageNamed:@"signin3"];
                        content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                        content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    }
                }
            }
            
            
        }
}

-(void)sureBtnClick:(UIButton *)sender
{
    [self getSignInHttpData];
    self.dontGotoShowDaySignInViewController(YES);
    
}
//签到
-(void)getSignInHttpData
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token= [user objectForKey:@"token"];
    [self.view isLoading:YES];
    [manager POST:[NSString stringWithFormat:@"%@%@",SignIn,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view isLoading:NO];
        [self.navigationController popViewControllerAnimated:YES];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"签到=======%@",dic);
        if (dic) {

            
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view isLoading:NO];
        [self.navigationController popViewControllerAnimated:YES];

        
        //存储当前时间
        NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSString *lastSignTime=[formatter stringFromDate:[NSDate date]];
        NSLog(@"第一次进入app的时间===%@",lastSignTime);
        NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
        [user setObject:lastSignTime forKey:@"lastSignTime"];
        [user synchronize];

        NSLog(@"error====%@",error.localizedDescription);
    }];
    
}














-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.hidden=YES;
   // [self.navigationController.navigationBar setTranslucent:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    
}






















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
